#!/usr/bin/env bash

# REQUIRES: preload,cuda
# RUN: bash %s %T/successful_build
# RUN: cd %T/successful_build; %{intercept-build} --cdb preload.json ./run.sh
# RUN: cd %T/successful_build; %{cdb_diff} --ignore-compiler preload.json expected.json

set -o errexit
set -o nounset
set -o xtrace

# the test creates a subdirectory inside output dir.
#
# ${root_dir}
# ├── run.sh
# ├── expected.json
# └── src
#    └── empty.cu

root_dir=$1
mkdir -p "${root_dir}/src"

touch "${root_dir}/src/empty.cu"

build_file="${root_dir}/run.sh"
cat > ${build_file} << EOF
#!/usr/bin/env bash

set -o nounset
set -o xtrace

nvcc -c -Dver=1 src/empty.cu;

cd src
nvcc -c -Dver=2 empty.cu;

true;
EOF
chmod +x ${build_file}

cat > "${root_dir}/expected.json" << EOF
[
{
  "arguments": [
    "nvcc",
    "-c",
    "-Dver=1",
    "src/empty.cu"
  ],
  "directory": "${root_dir}",
  "file": "src/empty.cu"
},
{
  "arguments": [
    "nvcc",
    "-c",
    "-Dver=2",
    "empty.cu"
  ],
  "directory": "${root_dir}/src",
  "file": "empty.cu"
}
]
EOF
